/********************************************************************************
 * Project Name		[JavaSE_Fundamentals]
 * File Name     	[GetSuperClassType.java]
 * Creation Date 	[2015-01-01]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ********************************************************************************/
package reflection;

/**
 * 反射 - 获得对象或类所属的Class类型
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 * 
 */
public class GetSuperClassType {

	/**
	 * 获得对象或类所属的Super Class类型
	 */
	public static void getSuperClass() {
		System.out.println("Hierarchy of class ......");

		// 注意这里使用class和type的区别
		// Class<?> basicType = Integer.TYPE;
		Class<?> basicType = Integer.class;
		System.out.println("Basic class type is " + basicType);
		Class<?> fatherClass = basicType.getSuperclass();
		System.out.println("Father class type is " + fatherClass);
		Class<?> GrandpaClass = fatherClass.getSuperclass();
		System.out.println("Grandpa class type is " + GrandpaClass);
	}

	public static void main(String[] args) throws ClassNotFoundException {
		// 方法1 类名.class
		Class<?> classType1 = String.class;
		// 方法2 对象名.getClass()
		Class<?> classType2 = new String().getClass();
		// 方法3 Class.forName(完整的类名)
		Class<?> classType3 = Class.forName("java.lang.String");
		// class之间的比较用 ==
		System.out.println("ClassType1 = " + classType1);
		System.out.println("ClassType2 = " + classType2);
		System.out.println("ClassType3 = " + classType3);

		// 基本类型的类型名 基本类型.class
		System.out.println("int.class is primitive -> " + int.class.isPrimitive());
		System.out.println("int[].class is primitive -> " + int[].class.isPrimitive());

		// 注意: 封装类的Class对象通过TYPE获得
		System.out.println("int.class == Integer.class -> " + (int.class == Integer.class));
		System.out.println("int.class == Integer.TYPE -> " + (int.class == Integer.TYPE));

		getSuperClass();

		// 在static方法中获取Class的类型
		Class<?> classType = new Object() {
		}.getClass().getEnclosingClass();
		System.out.println(classType);
	}
}
